package com.dgut.app.controller;

import com.dgut.app.param.CollectParam;
import com.dgut.app.service.OrderService;
import com.dgut.common.param.OrderFormParam;
import com.dgut.common.param.OrderListParam;
import com.dgut.common.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author zyq
 * @Date 2022/4/13 15:38
 * @Description TODO
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/app/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;


    /**
     * 用户发布回收订单
     * @param param 订单参数
     * @param token 用户令牌
     * @return R
     */
    @PostMapping
    public R publishOrder(@RequestBody @Valid OrderFormParam param, @RequestHeader("token") String token) {
        orderService.publishOrder(param, token);
        return R.ok();
    }


    /**
     * 订单列表
     * @param param 查询参数
     * @return R
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('1,2')")
    public R listOrder(OrderListParam param, @RequestHeader("token") String token) {
        return R.ok().data(orderService.listOrder(param, token));
    }


    /**
     * 订单详情信息
     * @param id 订单ID
     * @return R
     */
    @GetMapping("/detail/{id}")
    @PreAuthorize("hasAnyRole('1,2')")
    public R orderDetail(@PathVariable("id") String id) {
        return R.ok().data(orderService.detail(id));
    }


    /**
     * 回收员接取订单
     */
    @PutMapping("/accept/{id}")
    @PreAuthorize("hasAnyRole('2')")
    public R acceptOrder(@PathVariable("id") String id, @RequestHeader("token") String token) {
        orderService.accept(id, token);
        return R.ok();
    }

    /**
     * 回收员取消接单
     */
    @PutMapping("/cancel-accept/{id}")
    @PreAuthorize("hasAnyRole('2')")
    public R cancelAcceptOrder(@PathVariable("id") String id, @RequestHeader("token") String token) {
        orderService.cancelAccept(id, token);
        return R.ok();
    }

    /**
     * 回收员完成回收
     */
    @PostMapping("/collect")
    @PreAuthorize("hasAnyRole('2')")
    public R collectOrder(@RequestBody @Valid CollectParam param, @RequestHeader("token") String token) {
        orderService.collect(param, token);
        return R.ok();
    }

    /**
     * 发布者取消订单
     */
    @PutMapping("/cancel/{id}")
    @PreAuthorize("hasAnyRole('1,2')")
    public R cancelOrder(@PathVariable("id") String id, @RequestHeader("token") String token) {
        orderService.cancel(id, token);
        return R.ok();
    }

    /**
     * 发布者确认订单
     */
    @PutMapping("/confirm/{id}")
    @PreAuthorize("hasAnyRole('1,2')")
    public R confirmOrder(@PathVariable("id") String id, @RequestHeader("token") String token) {
        orderService.confirm(id, token);
        return R.ok();
    }

    /**
     * 发布者拒绝确认订单
     */
    @PutMapping("/cancel-confirm/{id}")
    @PreAuthorize("hasAnyRole('1,2')")
    public R cancelConfirmOrder(@PathVariable("id") String id, @RequestHeader("token") String token) {
        orderService.cancelConfirm(id, token);
        return R.ok();
    }



}
